package com.wztk.shydemo.yumi;

import android.graphics.YuvImage;

import com.fi.common.http.FiHttpRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created by win7 on 2016/7/18.
 */
public class InteractiveYumi {
    private String API_URL = "http://api.jyzszp.com/Api/index";
    private String uid = "";
    private String pwd = "";
    private String token = "";
    private static String error = "";

    //网络请求类
    protected class HttpRequestThread implements Callable<String> {
        private String url;
        private HashMap<String, String> hmParam;
        public HttpRequestThread(String url, HashMap<String, String> hmParam) {
            this.url = url;
            this.hmParam = hmParam;
        }

        @Override
        public String call() throws Exception {
           return FiHttpRequest.sendGet(this.url, this.hmParam);
        }
    }

    private InteractiveYumi(String uid, String pwd)  throws Exception {
        this.uid = uid;
        this.pwd = pwd;
        //this.token = getToken();
    }

    public static InteractiveYumi create(String uid, String pwd) throws Exception {
        if ("" == uid || null == uid || "" == pwd || null == pwd){
            return null;
        } else {
            InteractiveYumi ret = new InteractiveYumi(uid, pwd);
            if (ret.getToken())
                return  ret;
            else
                return null;
        }
    }

    //所有创建，请求错误都可以通过这个参数来取得
    public static String getError(){
        return error;
    }

    //新建一个线程进行HTTP请求，并且等待线程完成。
    private String newThreadHttpGet(String url, HashMap<String, String> hmParam) throws InterruptedException {
        String ret = "";
        HttpRequestThread httpRequestThread = new HttpRequestThread(url, hmParam);
        FutureTask<String> futureTask = new FutureTask<String>(httpRequestThread);
        Thread thread = new Thread(futureTask);
        thread.start();
        thread.join();
        try {
            ret = futureTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
            return ret;
        } catch (ExecutionException e) {
            e.printStackTrace();
            return ret;
        }
        return ret;
    }

    public String getVcodeAndReleaseMobile(String mobile, String pid) throws InterruptedException {
        String ret = "";
        String url = API_URL + "/getVcodeAndReleaseMobile";
        HashMap<String, String> hmParam = new HashMap<String, String>();
        hmParam.put("uid", this.uid);
        hmParam.put("token", this.token);
        hmParam.put("mobile", mobile);
        hmParam.put("pid", pid);

        ret = newThreadHttpGet(url, hmParam);

        switch (ret){
            case "not_receive":
            case "parameter_error":
            case "not_login":
            case "tokenaccount_is_locked":
            case "account_is_stoped":
            case "account_is_question_locked":
            case "account_is_ip_stoped":
            case "message|速度过快，请稍后再试":
            case "not_found_project":
            case "message|可使用余额不足":
            case "not_found_moblie":{
                error = ret;
                return null;
            }
            default:{
                return ret;
            }
        }







    }

    public List<String> getMobilenum(String pid, String mobile, String size) throws InterruptedException {
        List<String> retList;
        String ret = "";
        String url = API_URL + "/getMobilenum";
        HashMap<String, String> hmParam = new HashMap<String, String>();
        hmParam.put("pid", pid);
        hmParam.put("uid", this.uid);
        hmParam.put("token", this.token);

        if (!(null != mobile || "" != mobile))
            hmParam.put("mobile", mobile);
        if (!(null != size || "" != size))
            hmParam.put("size", mobile);
        ret = newThreadHttpGet(url, hmParam);
        switch (ret){
            case "no_data":
            case "parameter_error":
            case "not_login":
            case "tokenaccount_is_locked":
            case "account_is_stoped":
            case "account_is_question_locked":
            case "account_is_ip_stoped":
            case "message|速度过快，请稍后再试":
            case "not_found_project":
            case "message|可使用余额不足":
            case "max_count_disable":{
                error = ret;
                return null;
            }
            default:{
                retList = new ArrayList<String>(Arrays.asList(ret.split("\\|")));
                retList.remove(retList.size() - 1);
                return retList;
            }
        }
    }

    private boolean getToken() throws UnsupportedEncodingException, InterruptedException {
        String ret = null;
        String url = API_URL + "/loginIn";
        HashMap<String, String> hmParam = new HashMap<String, String>();
        hmParam.put("uid", this.uid);
        hmParam.put("pwd", this.pwd);
        ret = newThreadHttpGet(url, hmParam);
        String[] retArray  = ret.split("\\|");
        switch (retArray[0]) {
            case "login_error":
            case "message":
            case "account_is_locked":
            case "account_is_stoped":
            case "account_is_question_locked":
            case "account_is_ip_stoped":
            case "account_is_FreezeUser": {
                error = ret;
                return false;
            }
            default: {
                this.token = retArray[2];
                error = "";
                return true;
            }
        }
    }

}
